package com.jmu.content.controller.admin;


import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.github.pagehelper.util.StringUtil;
import com.jmu.constant.MessageConstant;
import com.jmu.content.dto.UserPageDTO;
import com.jmu.content.pojo.User;
import com.jmu.content.result.PageResult;
import com.jmu.content.result.Result;
import com.jmu.content.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author fems
 * @since 2024-05-06
 */
@RestController("adminUserController")
@RequestMapping("/admin/user")
public class UserController {

    @Autowired
    UserService userService;

    @GetMapping("/page")
    public Result<PageResult> pageResultResult(UserPageDTO dto){
        PageResult pageResult = userService.pageQuery(dto);
        return Result.success(pageResult);
    }


    @PutMapping
    public Result update(@RequestBody User user){
        LambdaUpdateWrapper<User> updateWrapper = new LambdaUpdateWrapper<>();
        String name = user.getName();
        String password = user.getPassword();
        String phone = user.getPhone();
        Integer status = user.getStatus();
        updateWrapper.eq(user.getId()!=null,User::getId,user.getId());
        updateWrapper.eq(StringUtil.isNotEmpty(name),User::getName,name);
        updateWrapper.eq(StringUtil.isNotEmpty(password),User::getPassword,password);
        updateWrapper.eq(StringUtil.isNotEmpty(phone),User::getPhone,phone);
        updateWrapper.eq(status!=null,User::getStatus,status);

        userService.update(updateWrapper);
        return Result.success();
    }


    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Long id){
        boolean bool = userService.removeById(id);
        if (!bool){
            return Result.error(MessageConstant.USER_NOT_FOUND);
        }
        return Result.success();
    }



}

